<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {include file="/layout/scriptload"}
    <title>系统管理</title>
</head>
<body>
<div class="box">
    <section class="content-header">
        <ol class="breadcrumb">
            <li><a><i class="fa fa-dashboard"></i> 公众号管理</a></li>
            <li class="active">分类管理</li>
        </ol>
    </section>
    <!-- /.box-header -->
    <section class="content">
        <div class="box-body">
            <div id="example1_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
                <div class="row">
                    <div class="col-sm-12">
                        <div id="example1_filter" class="dataTables_length">
                            <label>
                                <input type="search" id="keyword" class="form-control input-sm" placeholder="分类名称丶排序组" aria-controls="example1">
                            </label>
                            <button type="button" id="search" class="btn margin" style="background: #18bc9c;color: #ffffff;">搜索</button>
                            <button type="button" onclick="relist()" title="刷新" class="btn bg-aqua margin"><i class="fa fa-refresh"></i></button>
                            <button type="button" id="addcategory" class="btn margin" style="background: #18bc9c;color: #ffffff;">新增分类</button>
                            <button type="button" id="del_cate" class="btn bg-red margin"><i class="fa  fa-trash-o"></i> 删除</button>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <table id="example1" class="table table-bordered table-striped dataTable" role="grid" aria-describedby="example1_info">
                            <thead>
                            <tr role="row">
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 60px;">
                                    反选<input id="allchecked" type="checkbox" style="margin-left: 10px">
                                </th>
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 190px;">分类ID</th>
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 190px;">分类名称</th>
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 190px;">分类等级</th>
                                <th class="sorting_asc" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" aria-sort="ascending" style="width: 297px;">上级分类</th>
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 120px;">所属排序组</th>
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 200px;">创建时间</th>
                                <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1" style="width: 190px;">操作</th>
                            </tr>
                            </thead>
                            <tbody id="tb">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.box-body -->
    </section>
</div>
</body>
<script>
    var _keyword = '';
    $(function(){
        getlist(_keyword);
        $('#search').click(function(){
            table.destroy();
            var keyword = $('#keyword').val();
            getlist(keyword);
        })
        //反选
        $('#allchecked').click(function(){
            $("[name = checkboxed]:checkbox").each(function () {
                $(this).prop("checked", !$(this).prop("checked"));
            });
        })
        $('#addcategory').click(function(){
            layerWindow('分类管理->添加分类','{:url("tencent/doAddCategory")}','30%', '55%');
        })
        $('#refesh').click(function(){
            table.destroy();
            getlist(_keyword);
        })
        $('#del_cate').click(function(){
            var chk_value =[];
            $('input[name="checkboxed"]:checked').each(function(){
                chk_value.push($(this).val());
            });
            if(chk_value == ''){
                layerAlert('请选择需要删除的分类');
                return false;
            }
            $.ajax({
                type: 'post',
                cache: false,
                url: '{:url("admin/tencent/doDeleteCategory")}',
                data: {chk_value: chk_value},
                success: function (data) {
                    if(data.code == 0){
                        layerAlert('删除成功！',function () {
                            table.destroy();
                            getlist(_keyword);
                        });
                    }else{
                        layerAlert('删除失败');
                    }
                }
            })
        })
    })
    function toadd(_id,_pid){
        url = "{:url('tencent/addgroup')}" + "?categoryid=" + _id+"&pid="+_pid;
        layerWindow("添加账号",url,"80%","90%");
    }
    function toedit(_id){
        url = "{:url('tencent/editCategory')}" + "?categoryid=" + _id;
        layerWindow("编辑分类",url,"20%","40%");
    }
    function getlist(_keyword)
    {
        $.ajax({
            type: 'post',
            cache: false,
            url:'{:url("admin/tencent/getCategory")}',
            data: {keyword:_keyword},
            success: function(data) {
                if(data.code=="0")
                {
                    $("#tb tr").remove();
                    $.each(data.info,function(index,item){
                        if(item.level == 1)
                            var html='<tr role="row" class="odd" style="color: #18bc9c;font-weight: bold;" id="tr_'+item.id+'">';
                        else
                            var html='<tr role="row" class="odd" id="tr_'+item.id+'">';
                        html+='<td style="vertical-align: middle;"><input name="checkboxed" value="'+item.id+'" type="checkbox" style="margin-left: 10px"></td>';
                        html+='<td style="vertical-align: middle;">'+item.id+'</td>';
                        html+='<td style="vertical-align: middle;">'+item.categoryname+'</td>';
                        html+='<td style="vertical-align: middle;">'+item.level+'级分类</td>';
                        html+='<td style="vertical-align: middle;">'+item.superior+'</td>';
                        html+='<td style="vertical-align: middle;">'+item.sort+'</td>';
                        html+='<td style="vertical-align: middle;">'+item.createtime+'</td>';
                        html+='<td style="vertical-align: middle;text-align: center">';
                        html+='<div class="btn-group">';
                        html+=' <div title="编辑分类" onclick="toedit('+item.id+')" class="btn" style="background: #18bc9c;color: #ffffff;"><i class="fa fa-edit"></i></div>';
                        if(item.level=="2")
                            html+=' <button onclick="toadd('+item.id+','+item.pid+')" type="button" class="btn bg-aqua" title="添加账号"><i class="fa fa-plus-square"></i></button>';
                        html+='</div>';
                        html+='</td>';
                        html+='</tr>';
                        $("#tb").append(html);
                    });
                    table = $('#example1').DataTable({
                        "paging": true,
                        "lengthChange": false,
                        "lengthMenu": [[10],[10]],
                        "searching": false,
                        "ordering": false,
                        "info": true,
                        "language": {
                            "info": "展示第 <span style='color: red;'>_START_</span> 到 <span style='color: red;'>_END_</span> 个 共 <span style='color: red;'>_TOTAL_</span> 个公众号",
                            "paginate": {
                                "previous": "上一页",
                                "next": "下一页"
                            }
                        },
                        "autoWidth": true,
                    });
                }
            },
            error: function() {
                alert("发生系统错误,请与管理员联系！");
            }

        });
    }
    function relist()
    {
        table.destroy();
        getlist(_keyword);
    }
</script>
</html>